import csv


def handle(path):
    with open(path) as f:
        reader = csv.reader(f)
        data = list(reader)[1:]
        ranks = {}
        for each in data:
            date = each[0]
            name = each[1]
            if (date, name) in ranks:
                ranks[date, name] += int(each[2])
            else:
                ranks[date, name] = int(each[2])
    # print(ranks)
    temp = []
    temp0 = []
    for i in ranks.keys():
        temp.append(list(i))
        temp0.append(ranks[i])
    # print(temp)
    # print(temp0)
    temp1 = []
    temp2 = []
    for j in range(len(temp)):
        temp3 = temp[j]
        temp1.append(temp3[0])
        temp2.append(temp3[1])
    # print(temp1)
    # print(temp2)
    # print(temp0)
    outcome = []
    for k in range(len(temp)):
        part = []
        part.append(temp1[k])
        part.append(temp2[k])
        part.append(temp0[k])
        outcome.append(part)
    # print(outcome)

    header = ['date', 'name', 'value']

    with open("province_data.csv", 'w', newline='') as file:
        file_csv = csv.writer(file)
        file_csv.writerow(header)
        file_csv.writerows(outcome)


if __name__ == '__main__':
    path = "data.csv"
    handle(path)